#include "common.h"
#include "globals.h"
#include "logger.h"
#include "shared_func.h"
#include "termlist.h"
#include "termInverter.h"
#include "descriptorManager.h"

//-----globals---------
FILE* g_input;
char* fconf="./conf/inverter.ini";
TermInverter* tinv;
Globs g;
Stats s;
//---------------------

void usage() {
  fprintf(stderr, "\n");
  fprintf(stderr, "Project    : %s\n", PROJECT_NAME);
  fprintf(stderr, "BuildDate  : %s\n", BuildDate);
  fprintf(stderr, "Description: %s\n", DESCRIPTION);
  fprintf(stderr, "Usage: ./inverter\n");
  fprintf(stderr, "\n");
}

static int init_config() {
  IniContext iniContext;
  int ret;
  char* cstr;

  if((ret=iniLoadFromFile(fconf, &iniContext)) != 0) {
    FATAL("error load descriptor file:%s",
	fconf);
    return E_RET;
  }

  set_log_level(iniGetStrValue(NULL, "log_level", &iniContext));

  cstr = iniGetStrValue(NULL, "in_data_path", &iniContext);
  if(cstr == NULL) {
    FATAL("error get value : in_data_path");
    return E_RET;
  }
  strcpy(g.in_data_path,
      cstr);

  cstr = iniGetStrValue(NULL, "out_data_path", &iniContext);
  if(cstr == NULL) {
    FATAL("error get value : out_data_path");
    return E_RET;
  }
  strcpy(g.out_data_path,
      cstr);

  return S_RET;
}

int init() {
  int ret;
  log_init();
  g_log_context.log_level = LOG_DEBUG;
  memset(&g, 0, sizeof(g));
  memset(&s, 0, sizeof(s));
  
  ret = init_config();
  if(ret != S_RET) {
    FATAL("read config error");
    exit(-1);
  }

  tinv = new TermInverter();
  assert(tinv);

  ret = tinv->load();
  if(ret != S_RET) {
    FATAL("error load tinv");
    exit(-1);
  }

  return S_RET;
}

void do_it() {
  int fid = 0;
  char in_fname[NAME_LEN];
  char out_fname[NAME_LEN];
  int ret;
  FILE* fp;

  //check the first file
  sprintf(in_fname, "%s/%s.0.0",
      g.in_data_path,
      TERM_LIST_NAME);
  fp = fopen(in_fname, "r");
  if(fp == NULL) {
    WARNING("error fopen the [%s]", in_fname);
    exit(-1);
  }
  

  for(int i = 0 ; i < INDEX_LEVEL_NUM ; ++i) {
    while(1) {

      sprintf(in_fname, "%s/%s.%d.%d", 
	  g.in_data_path,
	  TERM_LIST_NAME, 
	  i, fid);

       sprintf(out_fname, "%s/%s.%d.%d", 
	  g.out_data_path,
	  INVERT_LIST_NAME, 
	  i, fid);

       NOTICE("processing [%s]->[%s]", 
	   in_fname, out_fname);

       ret = tinv->invert(in_fname, out_fname);
       if(ret == RET_NULL) {
//         NOTICE("end processing file(%s)",
//	     in_fname);
	 tinv->reset();
	 fid = 0;
	 break;
       }
       else if(ret != S_RET){
         FATAL("error invert");
	 exit(-1);
       }
       tinv->reset();
       fid++;
    }
  }
}


int main(int argc, char** argv) {
  int ret;

  g_input = stdin;

  ret = init();
  if(ret != S_RET) {
    FATAL("error init system");
    exit(-1);
  }

  if(argc == 2) {
    if(strcmp(argv[1], "-v") == 0 ||
	strcmp(argv[1], "--version") == 0 ||
	strcmp(argv[1], "-h") == 0) {
      usage();
      exit(-1);
    }
    g.mapid = atoi(argv[1]);
  }
  else if(argc == 1) {
    g.mapid = 0;
  }
  else {
    usage();
    exit(-1);
  }
  
  do_it();

  return S_RET;
}
